package ballParadigm;

import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.border.BevelBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JTextField;

public class freelanceGameFrame extends JFrame {
	
	//Game Objects Start
	ArrayList<GameObjects> gameObjects = new ArrayList<GameObjects>();
	ArrayList<JLabel> objectLabels = new ArrayList<JLabel>();
	ArrayList<Ball> myBall = new ArrayList<Ball>();
	ArrayList<JLabel> panelLabels = new ArrayList<JLabel>();
	ArrayList<GameObjects> panelObjects = new ArrayList<GameObjects>();
	//Game Objects End
	String backgroundimage;
	boolean newObjectActivated=false;
	GameObjects newGameObject;
	final Point hotspot = new Point(0, 0);
	Toolkit toolkit = Toolkit.getDefaultToolkit();
	boolean simulationOn=false;
	Timer timer;
	JPanel palette;
	JLayeredPane simulationPanel;
	JPanel panel;
	double gravity=0;
	collusion col = new collusion();
	int timertime = 50;
	int currentlevel = 0;
	boolean gameWon = false;
	int ballType = 1;
	
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					freelanceGameFrame frame = new freelanceGameFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public freelanceGameFrame() {
		

		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 900, 583);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null,
				null));
		panel.setBounds(717, 50, 177, 500);
		contentPane.add(panel);
		panel.setLayout(null);

		JLabel lblNewLabel_1 = new JLabel("Platte");
		lblNewLabel_1.setBounds(61, 24, 61, 16);
		panel.add(lblNewLabel_1);

		JButton btnStartSimulation = new JButton("Simulation Start/Pause");
		btnStartSimulation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(simulationOn==false){
					timer = new Timer();
					timer.scheduleAtFixedRate(new TimerTask() {

						@Override
						public void run() {


							for(int i=0; i<gameObjects.size();i++){
								if(col.collide(gameObjects.get(i), myBall.get(0))){
									gameObjects.get(i).ballTouched(col.collusionSide(gameObjects.get(i), myBall.get(0)), myBall, objectLabels, i);
								}
							}
							myBall.get(0).moveForward(gravity);
							objectLabels.get(0).setLocation((int)Math.round(myBall.get(0).x), (int)Math.round(myBall.get(0).y));
							if(myBall.get(0).y>500)
								myBall.get(0).dead=true;
							
							if(myBall.get(0).dead==true){
								timer.cancel();
								btnStartSimulation.disable();
							}
						}
					}, 100, 25);//First time elapse, second time interval
					simulationOn=true;
				}else{
					simulationOn=false;
					timer.cancel();
					simulationPanel.repaint();
					palette.repaint();
				}
			}
		});
		btnStartSimulation.setBounds(6, 434, 165, 50);
		panel.add(btnStartSimulation);
		
		palette = new JPanel();
		palette.setAutoscrolls(true);
		palette.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		palette.setBounds(6, 52, 165, 370);
		panel.add(palette);
		palette.setLayout(null);

		JButton btnRestartGame = new JButton("Restart Game");
		btnRestartGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setLevel1();
				btnStartSimulation.enable();
			}
		});
		btnRestartGame.setBounds(6, 16, 113, 22);
		contentPane.add(btnRestartGame);

		JButton btnSaveGame = new JButton("Save Game");
		btnSaveGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveGame.freelancemyBall.clear();
				saveGame.freelancegameObjects.clear();
				saveGame.freelancepanelObjects.clear();
				
				for(GameObjects o: gameObjects){
					GameObjects newObject = o;
					saveGame.freelancegameObjects.add(newObject);
				}
				for(Ball o: myBall){
					Ball newObject = o;
					saveGame.freelancemyBall.add(newObject);
				}
				for(GameObjects o: panelObjects){
					GameObjects newObject = o;
					saveGame.freelancepanelObjects.add(newObject);
				}
				saveGame.freelancebackgroundimage=backgroundimage;
			}
		});
		btnSaveGame.setBounds(131, 16, 113, 22);
		contentPane.add(btnSaveGame);

		JButton btnLoadGame = new JButton("Load Game");
		btnLoadGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loadGame();

			}
		});
		btnLoadGame.setBounds(256, 16, 113, 22);
		contentPane.add(btnLoadGame);

		JButton btnQuickLevel = new JButton("Quit Level");
		btnQuickLevel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnQuickLevel.setBounds(381, 16, 113, 22);
		contentPane.add(btnQuickLevel);
		
		simulationPanel = new JLayeredPane();
		simulationPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		simulationPanel.setBounds(6, 50, 699, 500);
		contentPane.add(simulationPanel);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(freelanceGameFrame.class.getResource("/ballParadigm/basketball.png")));
		lblNewLabel.setBounds(574, 10, 48, 33);
		contentPane.add(lblNewLabel);
		lblNewLabel.addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	ballType=1;
		    }  
		}); 
		
		JLabel lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.setIcon(new ImageIcon(freelanceGameFrame.class.getResource("/ballParadigm/bowlingball.png")));
		lblNewLabel_2.setBounds(644, 10, 48, 33);
		contentPane.add(lblNewLabel_2);
		lblNewLabel_2.addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	ballType=2;
		    }  
		}); 
		
		JLabel lblNewLabel_3 = new JLabel("");
		lblNewLabel_3.setIcon(new ImageIcon(freelanceGameFrame.class.getResource("/ballParadigm/pingpongball.png")));
		lblNewLabel_3.setBounds(768, 21, 48, 22);
		contentPane.add(lblNewLabel_3);
		lblNewLabel_3.addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	ballType=4;
		    }  
		}); 
		
		JLabel lblNewLabel_4 = new JLabel("");
		lblNewLabel_4.setIcon(new ImageIcon(freelanceGameFrame.class.getResource("/ballParadigm/tennisball.png")));
		lblNewLabel_4.setBounds(717, 22, 26, 16);
		contentPane.add(lblNewLabel_4);
		lblNewLabel_4.addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	ballType=3;
		    }  
		}); 
		
		simulationPanel.addMouseListener(new MouseAdapter() {
		    @Override
		    public void mouseClicked(MouseEvent e) {

		        if(newObjectActivated==true){

		        	newGameObject.x=e.getX();
		        	newGameObject.y=e.getY();
		        	gameObjects.add(newGameObject);
		        	objectLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(gameObjects.get(gameObjects.size()-1).image))));
		    		objectLabels.get(objectLabels.size()-1).setBounds((int)Math.round(gameObjects.get(gameObjects.size()-1).x), (int)Math.round(gameObjects.get(gameObjects.size()-1).y), gameObjects.get(gameObjects.size()-1).WIDTH, gameObjects.get(gameObjects.size()-1).HEIGHT);
		    		simulationPanel.add(objectLabels.get(objectLabels.size()-1),1);
		    		simulationPanel.repaint();
		    		newObjectActivated=false;
			    	simulationPanel.setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		        }
		    }
		});
		setLevel1();
	}
	public void setLevel1(){

			simulationPanel.removeAll();
			palette.removeAll();
			
			myBall.clear();
			objectLabels.clear();
			gameObjects.clear();
			panelObjects.clear();
			panelLabels.clear();
			
		gravity=0.1;
		//Inside the simulationPanel
		if(ballType==1){
			myBall.add(new Basketball(5, 25, 1,0));//Creates the ball and places it
			objectLabels.add(new JLabel(""));
			objectLabels.get(objectLabels.size()-1).setIcon(new ImageIcon(freelanceGameFrame.class.getResource(myBall.get(0).image)));
			objectLabels.get(objectLabels.size()-1).setBounds((int)Math.round(myBall.get(0).x), (int)Math.round(myBall.get(0).y), myBall.get(0).WIDTH, myBall.get(0).HEIGHT);
			simulationPanel.add(objectLabels.get(objectLabels.size()-1),1);
			simulationPanel.repaint();
		}else if(ballType==2){
			myBall.add(new Bowlingball(5, 25, 1,0));//Creates the ball and places it
			objectLabels.add(new JLabel(""));
			objectLabels.get(objectLabels.size()-1).setIcon(new ImageIcon(freelanceGameFrame.class.getResource(myBall.get(0).image)));
			objectLabels.get(objectLabels.size()-1).setBounds((int)Math.round(myBall.get(0).x), (int)Math.round(myBall.get(0).y), myBall.get(0).WIDTH, myBall.get(0).HEIGHT);
			simulationPanel.add(objectLabels.get(objectLabels.size()-1),1);
			simulationPanel.repaint();
		}else if(ballType==3){
			myBall.add(new Tennisball(5, 25, 1,0));//Creates the ball and places it
			objectLabels.add(new JLabel(""));
			objectLabels.get(objectLabels.size()-1).setIcon(new ImageIcon(freelanceGameFrame.class.getResource(myBall.get(0).image)));
			objectLabels.get(objectLabels.size()-1).setBounds((int)Math.round(myBall.get(0).x), (int)Math.round(myBall.get(0).y), myBall.get(0).WIDTH, myBall.get(0).HEIGHT);
			simulationPanel.add(objectLabels.get(objectLabels.size()-1),1);
			simulationPanel.repaint();
		}else if(ballType==4){
			myBall.add(new PingPongball(5, 25, 1,0));//Creates the ball and places it
			objectLabels.add(new JLabel(""));
			objectLabels.get(objectLabels.size()-1).setIcon(new ImageIcon(freelanceGameFrame.class.getResource(myBall.get(0).image)));
			objectLabels.get(objectLabels.size()-1).setBounds((int)Math.round(myBall.get(0).x), (int)Math.round(myBall.get(0).y), myBall.get(0).WIDTH, myBall.get(0).HEIGHT);
			simulationPanel.add(objectLabels.get(objectLabels.size()-1),1);
			simulationPanel.repaint();
		}
		//Inside panel
		
		panelObjects.add(new PlatformHorizontal(5, 20));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/platformhorizontal.png")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new PlatformHorizontal(0,0);//**changethis**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new Esnaf(5, 200));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/esnaf.gif")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new Esnaf(0,0);//**changethis**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new PlatformVertical(5, 150));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)  
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/platformvertical.png")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new PlatformVertical(0,0);//**change this**
		    	newObjectActivated = true;
		    }  
		}); 

		panelObjects.add(new SpringVertical(40, 150));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()
		{  
		    public void mouseClicked(MouseEvent e)  
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/springvertical.png")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new SpringVertical(0,0);//**change this**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new Teyze(40, 250));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)  
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/teyze.gif")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new Teyze(0,0);//**change this**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new SpringHorizontal(25, 28));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/springhorizontal.png")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new SpringHorizontal(0,0);//**changethis**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new Trampoline(40, 350));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/trampoline.gif")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new Trampoline(0,0);//**changethis**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new Wall(10, 50));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/wall.png")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new Wall(0,0);//**changethis**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new BarbedWire(10, 150));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/barbedwire.png")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new BarbedWire(0,0);//**changethis**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new TrafficSign(50, 200));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/trafficsign.png")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new TrafficSign(0,0);//**changethis**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new Window(100, 250));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/window.png")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new Window(0,0);//**changethis**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new Pot(100, 300));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/flowerpot.gif")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new Pot(0,0);//**changethis**
		    	newObjectActivated = true;
		    }  
		}); 
		
		panelObjects.add(new Cat(100, 150));//A panel and its action listener **change this**
		panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
		panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
		palette.add(panelLabels.get(panelLabels.size()-1));
		palette.repaint();

		panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
		{  
		    public void mouseClicked(MouseEvent e)
		    {  
		    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource("/ballParadigm/cat.gif")) , hotspot, "Mouseumla"));//**change image in this**
		    	newGameObject = new Cat(0,0);//**changethis**
		    	newObjectActivated = true;
		    }  
		}); 
		
		//Inside panel end
		backgroundimage="main.gif";
    	//setBackground(backgroundimage);
    	
    	currentlevel=1;
		
	}
	public void setBackground(String backgroundName){
		JLabel gameBackground = new JLabel("");
		gameBackground.setIcon(new ImageIcon(freelanceGameFrame.class.getResource(backgroundName)));
		gameBackground.setBounds(0, 0, 699, 500);
		simulationPanel.add(gameBackground,-5);
		simulationPanel.repaint();
	}
	public void loadGame(){
		simulationPanel.removeAll();
		palette.removeAll();
		simulationPanel.repaint();
		palette.repaint();
		myBall.clear();
		objectLabels.clear();
		gameObjects.clear();
		panelObjects.clear();
		panelLabels.clear();
		
		for(Ball o: saveGame.freelancemyBall){
			Ball newBall = o;
			myBall.add(newBall);//Creates the ball and places it
			objectLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(myBall.get(0).image))));
			objectLabels.get(objectLabels.size()-1).setBounds((int)Math.round(myBall.get(0).x), (int)Math.round(myBall.get(0).y), myBall.get(0).WIDTH, myBall.get(0).HEIGHT);
			simulationPanel.add(objectLabels.get(objectLabels.size()-1),1);
			simulationPanel.repaint();
		}
		for(GameObjects o: saveGame.freelancegameObjects){
			GameObjects newObject = o;
			gameObjects.add(newObject);//Creates the ball and places it
			objectLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(gameObjects.get(gameObjects.size()-1).image))));
			objectLabels.get(objectLabels.size()-1).setBounds((int)Math.round(gameObjects.get(gameObjects.size()-1).x), (int)Math.round(gameObjects.get(gameObjects.size()-1).y), gameObjects.get(gameObjects.size()-1).WIDTH, gameObjects.get(gameObjects.size()-1).HEIGHT);
			simulationPanel.add(objectLabels.get(objectLabels.size()-1),1);
			simulationPanel.repaint();
		}
		for(GameObjects o: saveGame.freelancepanelObjects){
			GameObjects newObject = o;
			panelObjects.add(newObject);//A panel and its action listener **change this**
			panelLabels.add(new JLabel(new ImageIcon(freelanceGameFrame.class.getResource(panelObjects.get(panelObjects.size()-1).image))));
			panelLabels.get(panelLabels.size()-1).setBounds((int)Math.round(panelObjects.get(panelObjects.size()-1).x), (int)Math.round(panelObjects.get(panelObjects.size()-1).y), panelObjects.get(panelObjects.size()-1).WIDTH, panelObjects.get(panelObjects.size()-1).HEIGHT);
			palette.add(panelLabels.get(panelLabels.size()-1));
			palette.repaint();

			panelLabels.get(panelLabels.size()-1).addMouseListener(new MouseAdapter()  
			{  
			    public void mouseClicked(MouseEvent e)  
			    {  
			    	simulationPanel.setCursor(getToolkit().createCustomCursor(toolkit.getImage(freelanceGameFrame.class.getResource(newObject.image)) , hotspot, "Mouseumla"));//**change image in this**
			    	newGameObject = new Teyze(0,0);//**change this**
			    	newObjectActivated = true;
			    }  
			}); 
		}
		backgroundimage=saveGame.freelancebackgroundimage;
		setBackground(backgroundimage);

	}
}
